var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var app = getApp();

Component({

    behaviors: [],

    properties: {
        isShow: {
            type: Boolean,
            value: false,
            observer: function(newVal, oldVal) {
                this.setData({
                    isShow: newVal
                });
            }
        },
    },
    data: {
        isShow: false
    },

    attached: function() {},
    moved: function() {},
    detached: function() {},

    methods: {
        close() {
            this.setData({
                isShow: false
            });
        },

        loginByWxPhone(e) {
            var {
                errMsg,
                iv,
                encryptedData
            } = e.detail;
            var {
                nickName,
                avatarUrl
            } = app.globalData.userInfo || {};

            if (!/:ok$/.test(errMsg)) {
                return;
            }
            ajax.P('/common/User/getUserPhone', {
                iv,
                encryptedData,
                nickName,
                avatarUrl
            }, (data) => {
                this._loginSuc(data);
            });
        },

        //登陆成功
        _loginSuc(data) {
            var userSid = data.result.userSid;
            var is_fenxiao = data.result.is_fenxiao;
            var user_id = data.result.user_id;

            app.setUserSid(userSid);
            app.setIsFenxiao(is_fenxiao);
            app.setUserId(user_id);

            this.close();

            this.triggerEvent('loginsuc', {});
        },
    }

})